package com.ruoyi.wq.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.GiveType;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqNoticeGive;
import com.ruoyi.wq.domain.vo.WqNoticeGiveVO;
import com.ruoyi.wq.mapper.WqNoticeGiveMapper;
import com.ruoyi.wq.service.IWqNoticeGiveService;

/**
 * 点赞通知Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-08
 */
@Service
public class WqNoticeGiveServiceImpl extends ServiceImpl<WqNoticeGiveMapper, WqNoticeGive> implements IWqNoticeGiveService {
    
	@Autowired
    private WqNoticeGiveMapper wqNoticeGiveMapper;
	
	@Autowired
	private ISysUserService sysUserService;

    /**
     * 查询点赞通知
     * 
     * @param id 点赞通知主键
     * @return 点赞通知
     */
    @Override
    public WqNoticeGive selectWqNoticeGiveById(String id){
        return wqNoticeGiveMapper.selectWqNoticeGiveById(id);
    }

    /**
     * 查询点赞通知列表
     * 
     * @param wqNoticeGive 点赞通知
     * @return 点赞通知
     */
    @Override
    public List<WqNoticeGive> selectWqNoticeGiveList(WqNoticeGive wqNoticeGive)
    {
        return wqNoticeGiveMapper.selectWqNoticeGiveList(wqNoticeGive);
    }

    /**
     * 新增点赞通知
     * 
     * @param wqNoticeGive 点赞通知
     * @return 结果
     */
    @Override
    public int insertWqNoticeGive(WqNoticeGive wqNoticeGive)
    {
        wqNoticeGive.setCreateTime(DateUtils.getNowDate());
        return wqNoticeGiveMapper.insertWqNoticeGive(wqNoticeGive);
    }

    /**
     * 修改点赞通知
     * 
     * @param wqNoticeGive 点赞通知
     * @return 结果
     */
    @Override
    public int updateWqNoticeGive(WqNoticeGive wqNoticeGive)
    {
        wqNoticeGive.setUpdateTime(DateUtils.getNowDate());
        return wqNoticeGiveMapper.updateWqNoticeGive(wqNoticeGive);
    }

    /**
     * 批量删除点赞通知
     * 
     * @param ids 需要删除的点赞通知主键
     * @return 结果
     */
    @Override
    public int deleteWqNoticeGiveByIds(String[] ids)
    {
        return wqNoticeGiveMapper.deleteWqNoticeGiveByIds(ids);
    }

    /**
     * 删除点赞通知信息
     * 
     * @param id 点赞通知主键
     * @return 结果
     */
    @Override
    public int deleteWqNoticeGiveById(String id)
    {
        return wqNoticeGiveMapper.deleteWqNoticeGiveById(id);
    }

	@Override
	public Integer getUnReadNum(Long userId) {
		WqNoticeGive noticeGive = new WqNoticeGive();
		QueryWrapper<WqNoticeGive> qw = Condition.getQueryWrapper(noticeGive);
		noticeGive.setIsRead("0");
		noticeGive.setNoticeUserId(userId);
		qw.setEntity(noticeGive);
		return baseMapper.selectCount(qw);
	}

	@Override
	public void deleteByDynamicIdAndUserId(String dynamicId, Long userId) {
		UpdateWrapper<WqNoticeGive> qw = new UpdateWrapper<>();
		WqNoticeGive noticeGive = new WqNoticeGive();
		noticeGive.setDynamicId(dynamicId);
		noticeGive.setGiveType(GiveType.DYNAMIC_GIVE.getCode());
		noticeGive.setUserId(userId);
		qw.setEntity(noticeGive);
		baseMapper.delete(qw);
	}

	@Override
	public void deleteByCommentIdAndUserId(String commentId, Long userId) {
		UpdateWrapper<WqNoticeGive> qw = new UpdateWrapper<>();
		WqNoticeGive noticeGive = new WqNoticeGive();
		noticeGive.setCommentId(commentId);
		noticeGive.setGiveType(GiveType.COMMENT_GIVE.getCode());
		noticeGive.setUserId(userId);
		qw.setEntity(noticeGive);
		baseMapper.delete(qw);
	}

	@Override
	public void deleteByCommentReplyIdAndUserId(String replyId, Long userId) {
		UpdateWrapper<WqNoticeGive> qw = new UpdateWrapper<>();
		WqNoticeGive noticeGive = new WqNoticeGive();
		noticeGive.setCommentReplyId(replyId);
		noticeGive.setGiveType(GiveType.REPLY_GIVE.getCode());
		noticeGive.setUserId(userId);
		qw.setEntity(noticeGive);
		baseMapper.delete(qw);
	}

	@Override
	public TableDataInfo getPageList(WqNoticeGive wqNoticeGive) {
		Long userId = SecurityUtils.getUserId();
		wqNoticeGive.setNoticeUserId(userId);
		QueryWrapper<WqNoticeGive> qw = Condition.getQueryWrapper(wqNoticeGive);
        qw.setEntity(wqNoticeGive);
        qw.orderByDesc("create_time");
        IPage<WqNoticeGive> page = baseMapper.selectPage(Condition.getPage(wqNoticeGive), qw);
        List<WqNoticeGiveVO> noticeVOList = new ArrayList<WqNoticeGiveVO>();
        for(WqNoticeGive notice : page.getRecords()){
        	WqNoticeGiveVO noticeVo = new WqNoticeGiveVO();
        	BeanUtils.copyProperties(notice, noticeVo);
        	noticeVo.setTime(DateUtils.getTime(notice.getCreateTime()));
        	noticeVOList.add(noticeVo);
        }
        return Condition.getTableDataInfo(noticeVOList, page.getTotal());
	}

	@Override
	public void updateGiveRead() {
		UpdateWrapper<WqNoticeGive> updateWrapper = new UpdateWrapper<>();
		WqNoticeGive entity = new WqNoticeGive();
		entity.setNoticeUserId(SecurityUtils.getUserId());
		entity.setIsRead("0");
		updateWrapper.setEntity(entity);
		
		WqNoticeGive noticeGive = new WqNoticeGive();
		noticeGive.setIsRead("1");
		baseMapper.update(noticeGive, updateWrapper);
		
		SysUser user = new SysUser();
		user.setUserId(SecurityUtils.getUserId());
		user.setGiveNewNum(0);
		sysUserService.updateById(user);
	}
}
